﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel.DomainServices.Server;
using System.ComponentModel.DataAnnotations;
using System.Linq;

namespace NetBay.Core.BusinessEntities.Workflow
{
    [DataContract]
    public class WorkflowModel : ReturnObject
    {
        #region Attributs
        private List<WorkflowAction> _actions = new List<WorkflowAction>();
        private List<WorkflowCondition> _conditions = new List<WorkflowCondition>();

        #endregion

        #region Properties

        /// <summary>
        /// Get or set the Identifier
        /// </summary>
        [DataMember]
        public long Identifier { get; set; }

        /// <summary>
        /// Get or set the WorkflowType
        /// </summary>
        [DataMember]
        public WorkflowType WorkflowType { get; set; }

        /// <summary>
        /// Get or set the ClassName
        /// </summary>
        [DataMember]
        public string ClassName { get; set; }

        /// <summary>
        /// Get or set the AssemblyName
        /// </summary>
        [DataMember]
        public string AssemblyName { get; set; }

        /// <summary>
        /// Get or set the Name
        /// </summary>
        [DataMember]
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the is system.
        /// </summary>
        /// <value>The is system.</value>
        [DataMember]
        public bool IsSystem { get; set; }

        /// <summary>
        /// Get or set the Description
        /// </summary>
        [DataMember]
        public string Description { get; set; }

        public override string ToString()
        {
            return string.Format("_actions: {0}, _conditions: {1}, Identifier: {2}, WorkflowType: {3}, ClassName: {4}, AssemblyName: {5}, Name: {6}, Description: {7}, Actions: {8}, Conditions: {9}, Rules: {10}", _actions, _conditions, Identifier, WorkflowType, ClassName, AssemblyName, Name, Description, Actions, Conditions, Rules);
        }

        /// <summary>
        /// Get or set the Actions
        /// </summary>
        [Association("FK_Actions", "Key", "ForeignKey")]
        [Include]
        [DataMember]
        public List<WorkflowAction> Actions
        {
            get
            {
                return _actions;
            }
            set
            {
                _actions = value;
                ItemEntity.SetForeignKey(_actions, Key);
            }
        }

        /// <summary>
        /// Get or set the Conditions
        /// </summary>
        [Association("FK_Conditions", "Key", "ForeignKey")]
        [Include]
        [DataMember]
        public List<WorkflowCondition> Conditions
        {
            get
            {
                return _conditions;
            }
            set
            {
                _conditions = value;
                ItemEntity.SetForeignKey(_conditions, Key);
            }
        }

        /// <summary>
        /// Model rules 
        /// </summary>
        [DataMember]
        public string Rules { get; set; }

        #endregion  

        #region Public Functions

        /// <summary>
        /// Gets the full name of the type.
        /// </summary>
        /// <returns></returns>
        public string GetFullTypeName()
        {
            return string.Format("{0}.{1}", AssemblyName, ClassName);
        }

        public override object Clone()
        {
            return this.MemberwiseClone();
        }

        #endregion

        #region Constructors
        
        public WorkflowModel()
        :base()
        {
            IsSystem = false;
        }

        /// <summary>
        /// Only for error
        /// </summary>
        /// <param name="ErrorMessage"></param>
        public WorkflowModel(string errorMessage, Exception applicationException = null)
        : base(errorMessage, applicationException)
        {
        }
        #endregion
    }

    public abstract class WorkflonModelDer : WorkflowModel
    {
        public abstract void SetActionsList(long wkfModelId);

        public abstract void SetConditionList(long wkfModelId);
    }
}